function ThunderWarningManager(){
	var panelUtil;
	var panelId = "affectObjDiv";
	var protectUtil = new ProtectionStatusEvaluation();
	var thunderPanel;
	
	var voltageSelector = (function(){
		var voltages = {'all':'全部','499':'500kV及以上','220':'220kV','110':'110kV'};
		var selectVoltage = '499';
		var component;
		var listener;
		function getComponent(callback){
			component = $('<select style="width:70px;font-style:normal;"/>');
			for(var i in voltages){
				var s = voltages[i];
				var item = $('<option value="'+i+'" >'+s+'</option>');
				if(i == selectVoltage){
					item.attr('selected','selected');
				}
				component.append(item);
			}
			listener = callback;
			return component;
		};
		function getCurrentVoltage(){
			return selectVoltage;	
		}
		function getCurrentVoltageName(){
			return voltages[selectVoltage];	
		}
		function addListener(){
			component.on('change',function(params){
				selectVoltage = params.target.value;
				listener(selectVoltage);
			});
		}
		return {
			getComponent : getComponent,
			currentVoltage:getCurrentVoltage,
			currentVoltageName:getCurrentVoltageName,
			addListener:addListener,
		};
	})();
	
	function DetailIcon(){
		var detail;
		function getComponent(text,num,title,callback){
			detail = $('<span class="pull-right"></span>');
			detail.append(text);
			if(num>0){
				detail.append($('<span title="'+title+'" style="margin:3px;" class="badge bg-red">'+num+'</span>'));
			}
			detail.click(function(){
				callback();
			});
			return detail;
		}
		return {
			getComponent:getComponent,
		};
	};
	this.show = function(){
		clearAll();
		panelUtil = new PanelTablesUtil(panelId);
		thunderPanel = new ThunderPanel();
		panelUtil.setTitle("雷击监测");
		panelUtil.setTitleTool(voltageSelector.getComponent(function(params){
			thunderPanel.changeVoltage(params);
		}));
		panelUtil.setInfo(queryDiv.getComponent);
		$.ajaxSettings.async = false;
		thunderPanel.show();
		$.ajaxSettings.async = true;
		panelUtil.show();
		thunderPanel.drawMap();
		queryDiv.addListener();
		voltageSelector.addListener();
	};
	var queryDiv = (function() {
		var component,restore,query,error,minus,start,end;
		var pattern = 'yyyy-MM-dd hh:mm';
		var queryGap;
		$.ajaxSettings.async = false;
		$.getJSON('/rest/thunder/getThunderWarningDefaultQueryGap',function(result){
			queryGap = result;
		});
		$.ajaxSettings.async = true;
		function getComponent(){
			component = $('<div class="panel-collapse collapse in" style="background:white" />');
			var now = new Date();
			var last = new Date(now.getTime()-queryGap*1000);
			var d = $('<div style="display:flex;"/>').appendTo(component);
			minus = $('<span class="btn btn-default" ><i class="fa fa-arrow-left"></i></span>').appendTo(d);
			start = $('<input type="text" class="form-control thunder-warning-start"/>').appendTo(d);
			end = $('<input type="text" class="form-control thunder-warning-end"/>').appendTo(d);
		//	restore = $('<span class="btn btn-default" ><i class="fa fa-repeat"></i></span>').appendTo(d);
			query = $('<span class="btn btn-default" >搜索</span>' ).appendTo(d);
			error = $('<div style="color:red" class="collapse">查询时间不得超过7天</div>').appendTo(component);
			
			start.val(DateUtil.pattern(last,pattern));
			end.val(DateUtil.pattern(now,pattern));
		//	start.val('2018-03-12 00:00');
		//	end.val('2018-03-15 00:00');
			return component;
		}
		function addListener(){
			var now = new Date();
			var last = new Date(now.getTime()-7*24*3600*1000);
			var startTime = DateUtil.pattern(last,pattern);
			var endTime = DateUtil.pattern(now,pattern);
			/*restore.click(function(){
				start.val(DateUtil.pattern(last,pattern));
				end.val(DateUtil.pattern(now,pattern));
			});*/
			minus.click(function(){
				var str = start.val();
				var d = new Date(str);
				d.setTime(d.getTime()-24*3600*1000);
				start.val(DateUtil.pattern(d,pattern));
			});
			query.click(function(){
				//将空的和不对的补齐
				var sta = start.val();
				var s = new Date(sta);
				if(!sta || s.toString() == 'Invalid Date'){
					sta = startTime;
					$('.thunder-warning-start').val(startTime);
					s = new Date(sta);
				}
				var en = $('.thunder-warning-end').val();
				var e = new Date(en);
				if(!en || e.toString() == 'Invalid Date'){
					en = endTime;
					$('.thunder-warning-end').val(endTime);
					e = new Date(en);
				}
				//判断是否在允许范围内
				if(Math.abs(e.getTime()-s.getTime())>=8*24*3600*1000){
					error.collapse('show');
				}else{
					error.collapse('hide');
					startTime = sta;
					endTime = en;
					thunderPanel.query();
				}
			});
			start.datetimepicker({
				format: 'yyyy-mm-dd hh:ii',
				autoclose : true,
				endDate : new Date(),
				todayHighlight : true,
			});
			end.datetimepicker({
				format: 'yyyy-mm-dd hh:ii',
				autoclose : true,
				endDate : new Date(),
				todayHighlight : true,
				pickerPosition:'bottom-left',
			});
		}
		function getQueryTime(){
			return [start.val(),end.val()];
		}
		return {
			getComponent:getComponent,
			addListener:addListener,
			getQueryTime:getQueryTime
		};
	})();
	
	function ThunderPanel(){
		var body;
		var title;
		function update(){
			if(body){
				body.html('');
				title.html('');
			}else{
				body = panelUtil.getCollapsePanelEle(null, "", true, null);
				title = body.prev('ul').find('li a');
			}
			queryThunder.show();
			mapPoints.update();
		}
		var queryThunder = (function(){
			function update(){
				drawLoading();
				var times = queryDiv.getQueryTime();
				var vol = voltageSelector.currentVoltage();
				$.getJSON("/rest/thunder/getThunderWarningTableData/"+times[0]+'/'+times[1]+'/'+vol,function(result){
					drawPanelTable(result.tableData);
					protectionStatus.update(result.protectionData);
				});
			}
			function show(){
				update();
			}
			function drawLoading(){
				var t = $('<span>雷击线路</span>');
				title.html(t);
				body.html(PanelTablesUtil.getNoDataDiv('加载中...'));
			}
			function drawPanelTable(data){
				var t = $('<span>雷击线路</span>');
				var d = new DetailIcon();
				t.append(d.getComponent('明细',data.length,'线路数量',function(){
					var url = '/ui/map/dialog/thunderwarning/thunderWarningList.xhtml?';
					var vol = voltageSelector.currentVoltage();
					var times = queryDiv.getQueryTime();
					url += 'startTime='+times[0]+'&endTime='+times[1]+'&voltage='+vol;
					var voltage = voltageSelector.currentVoltageName();
					if(voltage == '全部'){
						voltage = '';
					}
					MyModal.showModal(url, null, null, voltage+"一次设备雷击情况");
				}));
				title.html(t);
				if(data && data.length>0){
					panelUtil.setDangerBox();
					var tb = $('<table class="table table-bordered table-hover" />');
					body.html(tb);
					thunderTable.draw(data,['设备','雷击次数','详情'],tb);
				}else{
					body.html(PanelTablesUtil.getSmileInfo("无雷击情况"));
				}
			}
			return {
				update:update,
				show:show,
			};
		})();
		var thunderTable = (function(){
			var ele;
			var table;
			
			function draw(data,columns,div){
				var opt = MyTable.getDefaultOptions();
				opt.scrollY = 300;
				opt.scrollCollapse = true;
				var col = {className: "optionColumn", targets: 2 };
				if(opt.columnDefs){
					opt.columnsDefs.push(col);
				}else{
					opt.columnDefs = [col];
				}
				ele = div?div:ele;
				for(var i in data){
					data[i][3] = data[i][2];
					data[i][2] = '<button data-id="'+data[i][2]+'" data-name="'+data[i][0]+'" class="btn btn-primary fa fa-search"/>';
				}
				table = new MyTable(ele,columns,opt).setDataSet(data).show();
				ele.find('tbody').on('click', 'td.optionColumn', function(params) {
					params.stopPropagation();
					var eid = $(params.target).attr('data-id');
					var ename = $(params.target).attr('data-name');
					
					var times = queryDiv.getQueryTime();
					var url = "/ui/map/dialog/thunderwarning/thunderFaultDialog.xhtml?eid="+eid+"&ename="+encodeURI(encodeURI(ename))+'&startTime='
						+times[0]+'&endTime='+times[1];
					MyModal.showModal(url, null, null, ename+"雷击情况");
				});
				table.onRowClick(function(data){
					if(data){
						searchSelectObj(data[0],false,true,data[3]);
					}
				});
			}
			return {
				draw:draw
			};
		})();
		/**
		 * 需要外部的  电压等级  模式 查询时间
		 */
		var mapPoints = (function(){
			var points = [];
			function show(){
				update();
			}
			function clear(){
				weatherLayer.removeFeatures(points);
				points = [];
			}
			function update(){
				clear();
				var times = queryDiv.getQueryTime();
				var vol = voltageSelector.currentVoltage();
				$.getJSON("/rest/thunder/getThunderWarningMapData/"+times[0]+"/"+times[1]+'/'+vol,function(result){
					drawMapPoints(result,times);
				});
			}
			function drawMapPoints(data,times){
				//画点，当移动过时，出现pop，有且只有一个
				clear();
				for(var i in data){
					var d = data[i];
					var p = new SuperMap.Geometry.Point(Number(d[0]), Number(d[1]));
					var point = new SuperMap.Feature.Vector(p);
					point.style = {
							fillColor : "red",
							strokeColor : "yellow",
							pointRadius : 5
					};
					point.data = d;
					points.push(point);
				}
				weatherLayer.addFeatures(points);
				weatherLayer.setVisibility(true);
				selectFeature.activate();
			}
			return {
				show:show,
				clear:clear,
				update:update
			};
		})();
		var protectionStatus = (function(){
			var title;
			var body;
			function update(protectData){
				if(body){
					body.html('');
				}else{
					body = panelUtil.getCollapsePanelEle("iceProtection", "", false);
					title = body.prev('ul').find('li a');
				}
				var t = $('<span>保护状态评估</span>');
				title.html(t);
				protectUtil.setData(protectData);
				body.append(protectUtil.getDescDiv());
				body.append(protectUtil.getTableDiv());
			}
			function drawLoading(){
				var t = $('<span>覆冰线路</span>');
				title.html(t);
				body.html(PanelTablesUtil.getNoDataDiv('加载中...'));
			}
			function show(protectData){
				update(protectData);
			}
			return {
				update:update,
				show:show
			};
		})();
		return {
			changeModel:update,
			changeVoltage:update,
			query:update,
			show:update,
			drawMap:mapPoints.show,
		};
	};
};